package com.zhkj.lib.widget

import android.app.Activity
import android.content.Context
import android.content.res.ColorStateList
import android.util.AttributeSet
import android.util.TypedValue
import android.view.Gravity
import android.view.View
import android.widget.FrameLayout
import android.widget.TextView
import androidx.core.content.ContextCompat
import com.zhkj.lib.R
import com.zhkj.lib.utils.ScreenUtil

class TitleBar @JvmOverloads constructor(
    context: Context,
    attrs: AttributeSet? = null,
    defStyle: Int = 0
) : FrameLayout(context, attrs, defStyle) {

    private val defaultHeight = ScreenUtil.dip(context, 48)
    private val defaultPadding = ScreenUtil.dip(context, 10)
    private val defaultMargin = ScreenUtil.dip(context, 20)

    private val leftTextView: TextView = TextView(context).apply {
        setOnClickListener {
            if (context is Activity) context.onBackPressed()
        }
    }
    private val titleTextView: TextView = TextView(context)
    private val rightTextView: TextView = TextView(context)
    private val bottomDivider = View(context).apply {
        setBackgroundColor(ContextCompat.getColor(context, R.color.colorDivider))
        visibility = View.INVISIBLE
    }

    private var showLeft = false
    private var leftTextColor: ColorStateList? = null
    private var leftTextSize = 0F
    private var leftTextContent: String? = null
    private var leftDrawableResId = 0
    private var titleTextContent: String? = null
    private var titleTextSize = 0F
    private var titleTextColor: ColorStateList? = null
    private var showRight = false
    private var rightTextColor: ColorStateList? = null
    private var rightTextSize = 0F
    private var rightTextContent: String? = null
    private var rightDrawableResId = 0
    private var rightBackgroundResId = 0
    private var showDivider = false

    init {
        with(context.obtainStyledAttributes(attrs, R.styleable.TitleBar)) {
            showLeft = getBoolean(R.styleable.TitleBar_tb_show_left, false)
            leftTextColor = getColorStateList(
                R.styleable.TitleBar_tb_left_text_color
            )
            leftTextSize =
                getDimension(
                    R.styleable.TitleBar_tb_left_text_size,
                    ScreenUtil.sp(context, 12).toFloat()
                )
            leftTextContent = getString(R.styleable.TitleBar_tb_left_text)
            leftDrawableResId = getResourceId(R.styleable.TitleBar_tb_left_drawable, 0)
            titleTextContent = getString(R.styleable.TitleBar_tb_title_text)
            titleTextSize =
                getDimension(
                    R.styleable.TitleBar_tb_title_text_size,
                    ScreenUtil.sp(context, 17).toFloat()
                )
            titleTextColor = getColorStateList(R.styleable.TitleBar_tb_title_text_color)
            showRight = getBoolean(R.styleable.TitleBar_tb_show_right, false)
            rightTextColor = getColorStateList(R.styleable.TitleBar_tb_right_text_color)
            rightTextSize =
                getDimension(
                    R.styleable.TitleBar_tb_right_text_size,
                    ScreenUtil.sp(context, 12).toFloat()
                )
            rightTextContent = getString(R.styleable.TitleBar_tb_right_text)
            rightDrawableResId = getResourceId(R.styleable.TitleBar_tb_right_drawable, 0)
            rightBackgroundResId = getResourceId(R.styleable.TitleBar_tb_right_background, 0)
            showDivider = getBoolean(R.styleable.TitleBar_tb_show_divider, false)
            recycle()
        }

        with(leftTextView) {
            gravity = Gravity.CENTER
            setTextSize(TypedValue.COMPLEX_UNIT_PX, leftTextSize)
            compoundDrawablePadding = defaultPadding
            if (leftTextColor == null) {
                setTextColor(ContextCompat.getColor(context, R.color.colorDefaultText))
            } else setTextColor(leftTextColor)
            if (leftDrawableResId > 0) setComponentDrawable(leftDrawableResId, this)
            else text = leftTextContent
            visibility = if (showLeft) View.VISIBLE else View.INVISIBLE
            addView(
                this, LayoutParams(
                    LayoutParams.WRAP_CONTENT,
                    LayoutParams.MATCH_PARENT,
                    Gravity.START or Gravity.CENTER_VERTICAL
                ).apply {
                    leftMargin = defaultMargin
                }
            )
        }
        with(rightTextView) {
            gravity = Gravity.CENTER
            setTextSize(TypedValue.COMPLEX_UNIT_PX, rightTextSize)
            if (rightTextColor == null) {
                setTextColor(ContextCompat.getColor(context, R.color.colorDefaultText))
            } else setTextColor(rightTextColor)

            if (rightDrawableResId > 0) setComponentDrawable(rightDrawableResId, this)
            else text = rightTextContent
            if (rightBackgroundResId > 0) setBackgroundResource(rightBackgroundResId)
            visibility = if (showRight) View.VISIBLE else View.INVISIBLE
            setPadding(defaultPadding, 8, defaultPadding, 8)
            addView(
                this, LayoutParams(
                    LayoutParams.WRAP_CONTENT,
                    LayoutParams.WRAP_CONTENT,
                    Gravity.END or Gravity.CENTER_VERTICAL
                ).apply {
                    rightMargin = defaultMargin
                }
            )
        }
        with(titleTextView) {
            gravity = Gravity.CENTER
            setTextSize(TypedValue.COMPLEX_UNIT_PX, titleTextSize)
            if (titleTextColor == null) {
                setTextColor(ContextCompat.getColor(context, R.color.colorDefaultText))
            } else setTextColor(titleTextColor)
            text = titleTextContent
            addView(
                this,
                LayoutParams(LayoutParams.WRAP_CONTENT, defaultHeight, Gravity.CENTER)
            )
        }
        with(bottomDivider) {
            bottomDivider.visibility = if (showDivider) View.VISIBLE else View.INVISIBLE
            addView(this, LayoutParams(LayoutParams.MATCH_PARENT, 2, Gravity.BOTTOM))
        }
    }

    private fun setComponentDrawable(drawableResId: Int, target: TextView) {
        ContextCompat.getDrawable(context, drawableResId)?.run {
            setBounds(0, 0, intrinsicWidth, intrinsicHeight)
            target.setCompoundDrawables(this, null, null, null)
        }
    }

    fun showLeft(showLeft: Boolean): TitleBar {
        this.showLeft = showLeft
        leftTextView.visibility = if (showLeft) View.VISIBLE else View.INVISIBLE
        return this
    }

    fun setLeftTextSize(px: Float): TitleBar {
        this.leftTextSize = px
        leftTextView.setTextSize(TypedValue.COMPLEX_UNIT_PX, px)
        return this
    }

    fun setLeftTextColor(color: ColorStateList?): TitleBar {
        this.leftTextColor = color
        leftTextView.setTextColor(color)
        return this
    }

    fun setLeftTextContent(content: String?): TitleBar {
        this.leftTextContent = content
        leftTextView.text = content
        return this
    }

    fun setLeftIcon(resId: Int): TitleBar {
        if (leftDrawableResId != resId) {
            this.leftDrawableResId = resId
            setComponentDrawable(resId, leftTextView)
        }
        return this
    }

    fun setTitleTextColor(color: ColorStateList?): TitleBar {
        this.titleTextColor = color
        titleTextView.setTextColor(color)
        return this
    }

    fun setTitleTextSize(px: Float): TitleBar {
        this.titleTextSize = px
        titleTextView.setTextSize(TypedValue.COMPLEX_UNIT_PX, px)
        return this
    }

    fun setTitleTextContent(content: String?): TitleBar {
        this.titleTextContent = content
        titleTextView.text = content
        return this
    }

    fun showRight(showRight: Boolean): TitleBar {
        this.showRight = showRight
        rightTextView.visibility = if (showRight) View.VISIBLE else View.INVISIBLE
        return this
    }

    fun setRightTextSize(px: Float): TitleBar {
        this.rightTextSize = px
        rightTextView.setTextSize(TypedValue.COMPLEX_UNIT_PX, px)
        return this
    }

    fun setRightTextColor(color: ColorStateList?): TitleBar {
        this.rightTextColor = color
        rightTextView.setTextColor(color)
        return this
    }

    fun setRightTextContent(content: String?): TitleBar {
        this.rightTextContent = content
        rightTextView.text = content
        return this
    }

    fun setRightIcon(resId: Int): TitleBar {
        this.rightDrawableResId = resId
        setComponentDrawable(resId, rightTextView)
        return this
    }

    fun setRightButtonBackground(resId: Int) {
        rightTextView.setBackgroundResource(resId)
    }

    fun setLeftClickListener(onClickListener: OnClickListener): TitleBar {
        leftTextView.setOnClickListener(onClickListener)
        return this
    }

    fun setRightClickListener(onClickListener: OnClickListener): TitleBar {
        rightTextView.setOnClickListener(onClickListener)
        return this
    }

    fun setDividerVisible(visible: Boolean): TitleBar {
        bottomDivider.visibility = if (visible) View.VISIBLE else View.INVISIBLE
        return this
    }

    fun setFragmentStatusBar() {
        setPadding(0, defaultMargin, 0, 0)
    }

    fun setRightButtonEnable(enable: Boolean) {
        rightTextView.isEnabled = enable
    }
}